package ar.com.ford.it.fullEconomics.view.form;

import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesm01Currency;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;

public class CurrencyForm extends ValidatorForm {

    private String section;
    private String code;
    private String codeFind;
    private String fechaIn;
    private String fechaOut;
    private String editBlocked;
    private Collection currencys;
    private Teesm01Currency currencySelected;

    /**
     * Resetea el formulario
     * 
     */
    public void resetForm() {
        this.section = "";
        this.editBlocked = "false";
        this.code = null;
        this.codeFind = null;
        this.currencys = null;
        this.currencySelected = null;
        this.fechaIn = null;
        this.fechaOut = null;
    }

    /**
     * Limpia los campos utilizados en el form para la creacion o actualizacion
     * de Currencys
     * 
     */
    public void clearCreateUpdateFields() {
        this.code = null;
        this.fechaIn = null;
        this.fechaOut = null;
        this.editBlocked = "false";
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code.toUpperCase();
    }

    public String getCodeFind() {
        return codeFind;
    }

    public void setCodeFind(String codeFind) {
        this.codeFind = codeFind.toUpperCase();
    }

    public Collection getCurrencys() {
        return currencys;
    }

    public void setCurrencys(Collection currencys) {
        this.currencys = currencys;
    }

    /**
     * Setea el Currency seleccionado para luego ser modificado
     * 
     * @param key
     * @param value
     */
    public void setEditMapped(String key, String value) {
        List currencys = (List) this.getCurrencys();

        this.setCurrencySelected((Teesm01Currency) currencys.get(Integer
                .parseInt(key) - 1));
    }

    /**
     * Setea el Currency seleccionada para luego ser eliminado
     * 
     * @param key
     * @param value
     */
    public void setDeleteMapped(String key, String value) {
        setEditMapped(key, value);
    }

    public Teesm01Currency getCurrencySelected() {
        return currencySelected;
    }

    public void setCurrencySelected(Teesm01Currency currencySelected) {
        this.currencySelected = currencySelected;
    }

    /**
     * Setea los campos del cuadro de creacion/modificacion de Currencys
     * 
     * @param currency
     */
    public void setCurrencyForEdit(Teesm01Currency currency) {
        this.setCode(String.valueOf(currency.getEesm01CurrencyCodeC()));
        this.setFechaIn(FullEconomicsDateUtil.format(currency
                .getEesm01CurrencyInDateY()));
        this.setFechaOut(FullEconomicsDateUtil.format(currency
                .getEesm01CurrencyOutDateY()));
    }

    /**
     * Setea los campos del Currency para su posterior creacion
     * 
     * @return Currency con los campos minimos seteados para la posterior
     * creacion
     */
    public Teesm01Currency getCurrencyForCreate() {
        Teesm01Currency currency = new Teesm01Currency();
        currency.setEesm01CurrencyCodeC(this.getCode());
        currency.setEesm01CurrencyInDateY(FullEconomicsDateUtil.format(this
                .getFechaIn()));
        currency.setEesm01CurrencyOutDateY(FullEconomicsDateUtil.format(this
                .getFechaOut()));
        return currency;
    }

    /**
     * Setea los campos del Currency para su posterior actualizacion
     * 
     * @param currency
     * Currency a actualizar
     * @return Currency con los campos a actualizar modificados
     */
    public Teesm01Currency getCurrencyForUpdate(Teesm01Currency currency) {

        currency.setEesm01CurrencyInDateY(FullEconomicsDateUtil.format(this
                .getFechaIn()));
        currency.setEesm01CurrencyOutDateY(FullEconomicsDateUtil.format(this
                .getFechaOut()));
        return currency;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    /**
     * Genera las validaciones necesarias.
     */
    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {

        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);

        if ("createUpdate".equalsIgnoreCase(this.getSection())) {

            // Verifico el codigo que no este vacio
            if (StringUtils.isBlank(this.getCode())) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "errors.required", resources
                                .getMessage("currency.label.code")));
            }

            try {

                // Verifico y formateo la fechas In
                if (StringUtils.isBlank(this.getFechaIn())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.required",
                                            resources
                                                    .getMessage("currency.label.tableHeader.fechaIn")));
                } else {
                    if (FullEconomicsDateUtil.format(this.getFechaIn()) == null) {
                        ae
                                .add(
                                        ActionMessages.GLOBAL_MESSAGE,
                                        new ActionMessage(
                                                "errors.date",
                                                resources
                                                        .getMessage("currency.label.tableHeader.fechaIn")));
                    }
                }
            } catch (Exception e) {
                ae
                        .add(
                                ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "errors.date",
                                        resources
                                                .getMessage("currency.label.tableHeader.fechaIn")));
            }

            try {
                // Verifico y formateo la fechas Out
                if (StringUtils.isBlank(this.getFechaOut())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.required",
                                            resources
                                                    .getMessage("currency.label.tableHeader.fechaOut")));

                } else {
                    if (FullEconomicsDateUtil.format(this.getFechaOut()) == null) {
                        ae
                                .add(
                                        ActionMessages.GLOBAL_MESSAGE,
                                        new ActionMessage(
                                                "errors.date",
                                                resources
                                                        .getMessage("currency.label.tableHeader.fechaOut")));
                    }
                }
            } catch (Exception e) {
                ae
                        .add(
                                ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "errors.date",
                                        resources
                                                .getMessage("currency.label.tableHeader.fechaOut")));
            }

            // Si no hubo errores
            if (ae.size() == 0)
                // Verifico que la fecha de inicio sea menor a la fecha de fin
                if (FullEconomicsDateUtil.getDifference(this.getFechaIn(),
                        this.getFechaOut()) <= 0) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.date.range",
                                            resources
                                                    .getMessage("currency.label.tableHeader.fechaOut"),
                                            resources
                                                    .getMessage("currency.label.tableHeader.fechaIn")));
                }
        }

        return ae;
    }

    public String getFechaIn() {
        return fechaIn;
    }

    public void setFechaIn(String fechaIn) {
        this.fechaIn = fechaIn;
    }

    public String getFechaOut() {
        return fechaOut;
    }

    public void setFechaOut(String fechaOut) {
        this.fechaOut = fechaOut;
    }

    public String getEditBlocked() {
        return editBlocked;
    }

    public void setEditBlocked(String editBlocked) {
        this.editBlocked = editBlocked;
    }

}
